<?php

namespace app\index\controller\consulting;

use app\index\model\UM as UM;
use app\index\service\ConsultingService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use ShidianUtil\Utils\CheckUtil;
use think\APP;
use think\Request as Request;

/**
 * Consulting 控制器
 * Class Consulting
 * @package app\consulting\controller
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class Consulting
{
    // 请求参数
    private Request $request;

    // service
    private ConsultingService $service;

    public function __construct(Request $request, ConsultingService $consultingService)
    {
        $this->request = $request;
        $this->service = $consultingService;
    }


    /**
     * 咨询列表
     * @param \app\index\model\UM|null $user
     * @return ApiResponse
     * @url /consulting/consulting/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request = $this->request;

        if (is_null($user)) {
            return ApiResponse::success([]);
        }

        return ApiResponse::success($this->service->index($user, $request->param()));
    }

    /**
     * 咨询详情
     * @param \app\index\model\UM $user
     * @return ApiResponse
     * @url /consulting/consulting/read
     */
    public function read(UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');

        if (!CheckUtil::isId($id)) {
            return ApiResponse::fail('缺少id参数');
        }

        return ApiResponse::success($this->service->read($user, $id));
    }

}
